var now = getNowFormatDateTime(); 
var username = sessionStorage.getItem("name");
var userid = sessionStorage.getItem("userId");
var repairid;
var faultphoto = [];//拍照列表
var shopphoto = [];//拍照列表
var _config ;
var opt;
var insertJson ={};
var addJson={};
var calendar = new lCalendar();
var now = getNowFormatDateTime(); 
var dutyuser="";
calendar.init({
	'trigger': '#repairdate',
	'type': 'datetime',
	'maxDate': '2099-12-31',
	'minDate': now
});
function ICalendarClick(){
	
}
$(document).ready(function(){
	 $('#repairdate').val(now);
	$("#username").prepend(username);
	$("#repairtype").change(function(){
	  opt=$("#repairtype").val();
	  show(opt);
	});
	$.ajax({
			url: '../../repair_info/fillAdd'
			,type:'post'
			,dataType:'json'
			,contentType: "application/json; charset=utf-8"
			,data:JSON.stringify({})  
			,success: function(res){
				console.log(res);
				 $("#repairid").append(res.data.field.repairid);
				 repairid = res.data.field.repairid;
				 $("#repairtype1").empty();
				for(var i=0;i<res.data.select.equipment.length;i++){
					 $("#repairtype1").append('<option value="'+res.data.select.equipment[i].value1+'">'+res.data.select.equipment[i].key1+'</option>');
				}
		     	

			}
	});
});
$('#faultpic').on('click',function(){
	openphoto();
});
$('#shoppic').on('click',function(){
	openphoto1();
});
//打开钉钉拍照
function openphoto(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			faultphoto.push(result[0]);
			$('#faultpicUrl .clearfix').remove()
			$('#faultpicUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto(this)">'
					+'</div>'
					+'</div>');
			$('#faultpicUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}	
//打开钉钉拍照
function openphoto1(){
	if(_config == undefined){
		$.ajax({
			type : "post",
			data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26')}),
			contentType: "application/json; charset=utf-8",
			async: false,
			url : "../../dingding/getconfig",
			dataType : "json",
			success : function(suc) {
//				console.log(suc);
				_config = suc.config;
			}
		}); 
		dd.config({
			agentId : _config.agentid,
			corpId : _config.corpId,
			timeStamp : _config.timeStamp,
			nonceStr : _config.nonceStr,
			signature : _config.signature,
			jsApiList : [ 'runtime.info','biz.util.uploadImage' ]
		});
	}
	dd.ready(function(){
		dd.biz.util.uploadImage({
		    compression: false,//(是否压缩，默认为true)
		    quality: 100, // 图片压缩质量, 
		    resize: 100, // 图片缩放率
		    stickers: {   // 水印信息
		        time: getNowFormatDateTime(),
		        username: sessionStorage.getItem("name")
		    },
		    onSuccess : function(result) {
			shopphoto.push(result[0]);
			$('#shoppicUrl .clearfix').remove()
			$('#shoppicUrl').append('<div class="wig_img">'
					+'<div class="img_box">'
					+'<img alt="100x100" src="'+result[0]+'" onclick="showAllPic1(\''+result[0]+'\')"/>'
					+'<img class="close_b" src="../../genius/style/mobile_page/assets/images/close.png" onclick="deletePhoto1(this)">'
					+'</div>'
					+'</div>');
			$('#shoppicUrl').append('<div class="clearfix"></div>');
		    },
		    onFail : function(err) {}
		});
	});
}
//删除图片
function deletePhoto(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		faultphoto.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//删除图片
function deletePhoto1(obj){
	dd.device.notification.confirm({
	    message: "删除图片吗",
	    title: "提示",
	    buttonLabels: ['是', '否'],
	    onSuccess : function(result) {
	    	if(result.buttonIndex == '0'){
	    		var index = $(obj).parent().parent().index();
	    		$(obj).parent().parent().remove();
	    		shopphoto.splice(index,1);
	    	}
	        
	    },
	    onFail : function(err) {}
	});
	 
}
//预览图片
function showAllPic(imgOne){
	dd.biz.util.previewImage({
		urls: faultphoto,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}
//预览图片
function showAllPic1(imgOne){
	dd.biz.util.previewImage({
		urls: shopphoto,//图片地址列表
		current: imgOne,//当前显示的图片链接
		onSuccess : function(result) {
			/**/
		},
		onFail : function(err) {}
	})
}
function goback(){
	history.go(-1);
}
function finish(flag){
		getduty();
		insertJson.repairid=repairid;
		insertJson.repairtype=$('#repairtype').val();
		insertJson.analysis=$('#analysis').val();
		insertJson.plan=$('#plan').val();
		insertJson.lrr=userid;
	 	if($('#repairtype').val()=='2'){
			insertJson.reply=$('#reply').val();
	 	}else if($('#repairtype').val()=='3'){
		insertJson.faultpic = faultphoto.join(',');
		insertJson.shoppic = shopphoto.join(',');
		insertJson.returned = getflag($("#returned")[0].checked);
		insertJson.partsflag = getflag($("#partsflag")[0].checked);
		insertJson.parts = $('#parts').val();
		insertJson.cost_worker = $('#cost_worker').val();
		insertJson.cost_parts = $('#cost_parts').val();
		insertJson.qita = $('#qita').val();
	 }
	 if($('#repairtype').val()==3 && flag == 3){
			if($("#agree")[0].checked){
				$.ajax({
					url: "../../repair_info/weixiu_add"
					,type:'post'
					,dataType:'json'
					,async: false
					,contentType: "application/json; charset=utf-8"
					,data:JSON.stringify(insertJson) 
					,success: function(res){
						finish2(flag);
					}					
				});
			}else{
				layer.msg("请确认客户清楚并认同维修过程和结果及费用");
			}
		}else{
		    $.ajax({
				url: "../../repair_info/weixiu_add"
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(insertJson) 
				,success: function(res){
					finish2(flag);
				}					
			});
		}		
}
function finish2(flag){
		addJson.repairid=repairid;
		addJson.repairdate=$('#repairdate').val();
		addJson.baoxiutype=$('#baoxiutype').val();
		addJson.shopcode=$('#shopcode').val();
		addJson.tel=$('#tel').val();
		addJson.repairtype=$('#repairtype1').val();
		addJson.instruction=$('#instruction').val();
		addJson.lrr=userid;
		addJson.flag=flag;
		addJson.dutyuser=userid;
		if(flag==22){
			addJson.dutyuser=dutyuser;
			addJson.paidanflag=1;
		}
		$.ajax({
				url: "../../repair_info/add"
				,type:'post'
				,dataType:'json'
				,async: false
				,contentType: "application/json; charset=utf-8"
				,data:JSON.stringify(addJson) 
				,success: function(res){
					
				}					
		});	
	$.ajax({
		url: '../../repair_info/jiedan'
		,type:'post'
		,dataType:'json'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({'repairid':repairid,'flag':flag})  
		,success: function(res){
			if(res.status==1002){
				layer.msg("操作成功",{time:1000},function(){
					if(flag==22){
						usid = dutyuser;
						ding();
					}
					var url= "../../mobile_page/index_weixiu";
					window.location.href = url;
				});
			}
		}
	});
}
function getflag(flag){
	if(flag){return 0;}else{return 1;}
}
function show(opt){
	  if(opt=='1'){
		$("#replydiv")[0].style.display="none";
		$("#weixiudiv")[0].style.display="none";
	  }else if(opt=='2'){
		$("#replydiv")[0].style.display="";
		$("#weixiudiv")[0].style.display="none";
	  }else if(opt=='3'){
		$("#replydiv")[0].style.display="none";
		$("#weixiudiv")[0].style.display="";
	  }
}
//提交
function getduty(){
	$.ajax({
		url: '../../repair_info/getduty'
		,type:'post'
		,dataType:'json'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"shopcode":$('#shopcode').val()})  
		,success: function(res){
			dutyuser = res.data[0].dutyuser;
		}
	});
}
function ding(){
	$.ajax({
		url: '../../repair_info/remind'
		,type:'post'
		,dataType:'json'
		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify({"agentid":"1430279035","userid":usid,"url":"http://123.56.177.178:8383/unite_platform/mobile_page/index_weixiu?v="+Math.random(),"title":"门店报修通知","text":"报修单号"+repairid})  
		,success: function(res){
		}
	});
	layer.msg("操作成功",{time:1000},function(){
			history.go(-1);
	});
}